home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / ddjcompr / ashford / files.c < prev    next >
C/C++ Source or Header  |  1991-04-28  |  3KB  |  191 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <io.h>
  4. #include <fcntl.h>
  5. #include <sys\stat.h>
  6.  
  7. #include "files.h"
  8.  
  9. #define OUT_BUF_MAX     (2 * OUT_BUF_SIZE)
  10.  
  11.  
  12. static unsigned long input_file_length = 0;
  13. static unsigned long output_file_length = 0;
  14.  
  15. static int input_file;
  16. static int output_file;
  17.  
  18. static unsigned char output_area [OUT_BUF_MAX];
  19. static int output_index;
  20. static int output_max;
  21.  
  22. static unsigned char input_area [IN_BUF_SIZE];
  23. static int input_index;
  24. static int input_len;
  25.  
  26.  
  27. static void read_input_area (void);
  28. static void write_output_area (void);
  29.  
  30.  
  31. long OpenInputFile (char *fn)
  32.  
  33. {
  34.     long filesize;
  35.  
  36.     input_file = open (fn, O_RDONLY | O_BINARY);
  37.     if (input_file < 0)
  38.     {
  39.         perror ("Error on file open");
  40.         exit (EXIT_FAILURE);
  41.     }
  42.  
  43.     filesize = lseek (input_file, 0L, SEEK_END);
  44.     lseek (input_file, 0L, SEEK_SET);
  45.  
  46.     read_input_area ();
  47.  
  48.     return filesize;
  49. }
  50.  
  51.  
  52. int ReadInputFile (void)
  53.  
  54. {
  55.     if (input_index == input_len)
  56.     {
  57.         read_input_area ();
  58.         if (input_len == 0) return -1;
  59.     }
  60.  
  61.     return input_area [input_index ++];
  62. }
  63.  
  64.  
  65.  
  66. int ResetOutputPointer (unsigned pos)
  67.  
  68. {
  69.     output_index -= pos;
  70.     if (output_index < 0)
  71.     {
  72.         output_index += OUT_BUF_MAX;
  73.         output_file_length -= OUT_BUF_MAX;
  74.     }
  75.  
  76.     return output_area [output_index];
  77. }
  78.  
  79.  
  80. void OpenOutputFile (char *fn)
  81.  
  82. {
  83.     output_file = open (fn, O_RDWR+O_BINARY+O_CREAT+O_TRUNC, S_IREAD+S_IWRITE);
  84.     if (output_file < 0)
  85.     {
  86.         perror ("Error on output file open");
  87.         exit (EXIT_FAILURE);
  88.     }
  89.  
  90.     output_file_length = 0;
  91.     output_index = 0;
  92.     output_max = OUT_BUF_MAX;
  93. }
  94.  
  95.  
  96.  
  97. void WriteOutputFile (int ch)
  98.  
  99. {
  100.     output_area [output_index] = ch;
  101.  
  102.     output_index ++;
  103.     if (output_index == output_max)
  104.         write_output_area ();
  105.     else
  106.     if (output_index == OUT_BUF_MAX)
  107.     {
  108.         output_index = 0;
  109.         output_file_length += OUT_BUF_MAX;
  110.     }
  111. }
  112.  
  113.  
  114.  
  115. void CloseInputFile (void)
  116.  
  117. {    close (input_file);
  118. }
  119.  
  120.  
  121.  
  122. void CloseOutputFile (void)
  123.  
  124. {
  125.     if (output_index < output_max)
  126.     {
  127.         write (output_file, &output_area [output_max], OUT_BUF_MAX - output_max);
  128.         output_max = 0;
  129.     }
  130.  
  131.     write (output_file, &output_area [output_max], output_index - output_max);
  132.     close (output_file);
  133. }
  134.  
  135.  
  136.  
  137. static void read_input_area ()
  138.  
  139. {
  140.     input_file_length += input_len;
  141.     input_index = 0;
  142.     input_len = read (input_file, input_area, IN_BUF_SIZE);
  143.  
  144.     if (input_len < 0)
  145.     {
  146.         perror ("Error reading input file");
  147.         exit (EXIT_FAILURE);
  148.     }
  149. }
  150.  
  151.  
  152. static void write_output_area (void)
  153.  
  154. {
  155.     int n;
  156.  
  157.     if (output_max == OUT_BUF_MAX)
  158.     {
  159.         output_file_length += OUT_BUF_MAX;
  160.         output_max = OUT_BUF_SIZE;
  161.         output_index = 0;
  162.     }
  163.     else
  164.         output_max += OUT_BUF_SIZE;
  165.  
  166.     n = write (output_file, &output_area [output_index], OUT_BUF_SIZE);
  167.     if (n < 0)
  168.     {
  169.         perror ("\nError writing output file");
  170.         exit (EXIT_FAILURE);
  171.     }
  172.     else
  173.     if (n < OUT_BUF_SIZE)
  174.     {
  175.         printf ("\nDisk full on write\n");
  176.         exit (EXIT_FAILURE);
  177.     }
  178. }
  179.  
  180.  
  181. unsigned long GetOutputLength (void)
  182.  
  183. {    return output_file_length + output_index;
  184. }
  185.  
  186.  
  187. unsigned long GetInputLength (void)
  188.  
  189. {    return input_file_length + input_index;
  190. }
  191.